'''
Created on 07/06/2010

@author: Will
'''
from __future__ import division
from pyglet import clock
from view.camera import Camera
from view.hud import Hud
from utils import globals
from core.world import World
from view.gameWindow import GameWindow

import pyglet



class App(object):

    def __init__(self):
        self.world = World()                        #should be just something to conatin objects and physics!
        self.win = GameWindow()                     #the window!
        self.camera = Camera(self.win, zoom=600)    #the camera show on the window!
        self.hud = globals.hudInstance = Hud(self.win,self.camera)        #the heads up display
        self.keyboard = pyglet.window.key.KeyStateHandler()
        
        self.win.notifyMouseEventsTo(self.hud)# for wathever it might use it =|
        self.win.notifyMouseEventsTo(self.camera) # for zooming
        self.win.notifyMouseEventsTo(self) # for clicking on objects
        self.win.push_handlers(self.keyboard)
        
        self.world.createEnts(self.camera,self.win)

    def mainLoop(self):
        while not self.win.has_exit:            
            self.win.dispatch_events()
            self.world.tick(self.keyboard)
            clock.tick()
            
            self.camera.worldProjection()
            self.world.draw()
            
            self.camera.hudProjection()
            self.hud.draw()
            self.updateCameraPos()            
            
            self.win.flip()            
    
    def updateCameraPos(self):
        crosshairPos = self.world.getCrosshairPosition()
        playerpos = self.world.cameraFocus.x,self.world.cameraFocus.y
        self.camera.x = (crosshairPos[0] + playerpos[0])/2
        self.camera.y = (crosshairPos[1] + playerpos[1])/2
        
    def on_mouse_motion(self, x, y, dx, dy):    
        pass        

    def on_click_at_car(self,car):
        
        self.world.getPlayerIntoOrOut(car)
            
    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
        pass     

    def on_mouse_press(self, x, y, button, modifiers):
        for obj in self.world.getObjectsAtCrosshair():
                obj.on_click()
                if obj.isCar:
                    self.on_click_at_car(obj)
                
    def on_mouse_scroll(self,x, y, scroll_x, scroll_y):
        pass

app = App()
app.mainLoop()

